<template>
  <van-submit-bar :price="sum" button-text="提交订单" @submit="onSubmit">
    <van-checkbox v-model:checked="hasChecked" @click="checkAllBtn"
      >全选</van-checkbox
    >
  </van-submit-bar>
</template>

<script>
import { mapMutations, mapState } from "vuex";
// import { ref } from "vue";
export default {
  setup() {},
  data() {
    return {
      // checked: false,
    };
  },
  computed: {
    ...mapState("cart", ["cartDetailList"]),
    hasChecked() {
      let flag = true;
      this.cartDetailList.forEach((val) => {
        if (val.pChecked === false) {
          flag = false;
        }
      });
      return flag;
    },
    sum() {
      let sum = 0;
      this.cartDetailList.forEach((val) => {
        if (val.pChecked) {
          sum += val.currentPrice * val.productNum;
        }
      });
      return sum * 100;
    },
  },
  methods: {
    ...mapMutations("cart", ["checkAll"]),
    //全选函数
    checkAllBtn() {
      this.checkAll({ pchecked: !this.hasChecked });
    },
    //提交订单
    onSubmit() {
      //判断是否有选中商品
      let flag = false;
      this.cartDetailList.forEach((val) => {
        if (val.pChecked === true) {
          flag = true;
        }
      });
      //路由跳转
      if (flag) {
        this.$router.push({ path: "/conorder" });
      }
    },
  },
};
</script>

<style></style>
